#include <vector>
using namespace std;
class Solution
{
public:
    int tmp[50010] = {0};
    void merge_sort(vector<int> &nums, int left, int right, long long &ans)
    {
        if (left >= right)
            return;
        int mid = (left + right) >> 1;
        merge_sort(nums, left, mid, ans);
        merge_sort(nums, mid + 1, right, ans);
        // 到此左右两个区间是有序的，需要合并
        // cout << left << ' ' << mid  << ' ' << right << endl;
        int l1 = left, l2 = mid + 1;
        int k = left;
        int j = left;
        for (int i = mid + 1; i <= right; ++i)
        {
            while (j <= mid && nums[j] <= (long long)nums[i] * 2)
                ++j;
            // 循环结束，j指向第一个大于nums[i]两倍的数
            ans += mid - j + 1;
        }
        while (l1 <= mid && l2 <= right)
        {
            if (nums[l1] >= nums[l2])
            {
                //   //找比l2的两倍大的位置
                //   int ll = left, rr = mid;
                //   while(ll < rr)
                //   {
                //     int mid2 = ll + rr >> 1;
                //     if(nums[mid2] > (long long)nums[l2] * 2) rr = mid2;
                //     else ll = mid2 + 1;
                //   }
                //   if(nums[ll] > (long long)nums[l2] * 2) ans += mid + 1 - ll;
                tmp[k++] = nums[l2++];
            }
            else
                tmp[k++] = nums[l1++];
        }
        while (l1 <= mid)
            tmp[k++] = nums[l1++];
        while (l2 <= right)
            tmp[k++] = nums[l2++];
        for (int i = left; i <= right; ++i)
            nums[i] = tmp[i];
    }
    // 由于负数的特殊性，我们需要先统计个数然后才能进行排序
    int reversePairs(vector<int> &nums)
    {
        long long ans = 0;
        merge_sort(nums, 0, nums.size() - 1, ans);
        return ans;
    }
};